package com.wutianyi.images;

import java.awt.Frame;
import java.awt.image.renderable.ParameterBlock;
import java.io.IOException;

import javax.media.jai.Interpolation;
import javax.media.jai.JAI;
import javax.media.jai.RenderedOp;
import javax.media.jai.widget.ScrollingImagePanel;

import com.sun.media.jai.codec.FileSeekableStream;

public class JAISampleProgram
{
    public static void main(String[] args)
    {
        /*
         * Create an input stream from the specified file name to be used with
         * the file decoding operator.
         */
        FileSeekableStream stream = null;
        try
        {
            stream = new FileSeekableStream("sina.png");
        }
        catch (IOException e)
        {
            e.printStackTrace();
            System.exit(0);
        }
        /* Create an operator to decode the image file. */
        RenderedOp image1 = JAI.create("stream", stream);
        /*
         * Create a standard bilinear interpolation object to be used with the
         * "scale" operator.
         */
        Interpolation interp = Interpolation.getInstance(Interpolation.INTERP_BILINEAR);
        /**
         * Stores the required input source and parameters in a ParameterBlock
         * to be sent to the operation registry, and eventually to the "scale"
         * operator.
         */
        ParameterBlock params = new ParameterBlock();
        params.addSource(image1);
        params.add(2.0F); // x scale factor
        params.add(2.0F); // y scale factor
        params.add(0.0F); // x translate
        params.add(0.0F); // y translate
        params.add(interp); // interpolation method
        /* Create an operator to scale image1. */
        RenderedOp image2 = JAI.create("scale", params);
        /* Get the width and height of image2. */
        int width = image2.getWidth();
        int height = image2.getHeight();
        /* Attach image2 to a scrolling panel to be displayed. */
        ScrollingImagePanel panel = new ScrollingImagePanel(image2, width, height);
        /* Create a frame to contain the panel. */
        Frame window = new Frame("JAI Sample Program");
        window.add(panel);
        window.pack();
        window.show();
    }
}
